package com.ylsk.check.task;

import com.alibaba.fastjson.JSONObject;
import com.ylsk.check.domain.dto.SendMsgArgsDTO;
import com.ylsk.check.domain.so.CheckRecordSO;
import com.ylsk.check.domain.vo.ReportInfoVO;
import com.ylsk.check.service.ICheckRecordService;
import com.ylsk.check.service.impl.ReportInfoHelper;
import com.ylsk.check.util.SendMsg;
import com.ylsk.common.core.domain.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 检索未执行任务
 */
@Component("retrieveUnexecutedTask")
public class RetrieveUnexecutedTask {

    private static final Logger log = LoggerFactory.getLogger(RetrieveUnexecutedTask.class);
    @Autowired
    private ICheckRecordService checkRecordService;

    @Autowired
    private ReportInfoHelper helper;

    /**
     * 获取未检查数据 并发送短信息
     */
    public void sendToMsg(){
        CheckRecordSO checkRecordSO = new CheckRecordSO();
        List<ReportInfoVO> todayCheckData = checkRecordService.getTodayCheckData(checkRecordSO);
        //按照大队的维度 进行分组运用
        Map<Long, List<ReportInfoVO>> uncheckDataMap = todayCheckData.stream().collect(Collectors.groupingBy(ReportInfoVO::getDeptId));
        if(CollectionUtils.isEmpty(uncheckDataMap)){
            return;
        }
        log.info("开始发送短信,截止目前未检查项目数量为{}",uncheckDataMap.size());
        for (Map.Entry<Long, List<ReportInfoVO>> entry : uncheckDataMap.entrySet()) {
//            //发送消息通知
//            List<Long> toUser = helper.getDeptLeader(entry.getKey()).stream().map(SysUser::getUserId).collect(Collectors.toList());
//            MsgTemplateDTO msgTemplateDTO = new MsgTemplateDTO(MsgTemplateEnum.TEMPLATE_UN_CHECK_TIP, GlobalConstants.ADMIN_ID, toUser);
//            Integer p1 = entry.getValue().size();
//            helper.saveMsg(msgTemplateDTO,p1);

            //发送短信
            List<Long> toUser = helper.getDeptLeader(entry.getKey()).stream().map(SysUser::getUserId).collect(Collectors.toList());
            for (int i = 0; i < toUser.size(); i++) {
                SysUser sysUser = helper.getUserInfo(toUser.get(i));
                SendMsgArgsDTO sendMsgArgsDTO = new SendMsgArgsDTO();
                sendMsgArgsDTO.setTellPhone(sysUser.getPhonenumber());
                sendMsgArgsDTO.setProject(SendMsgArgsDTO.PROJECT_SGTX);

                JSONObject vars = new JSONObject();
                vars.put("num",entry.getValue().size());
                vars.put("deptName",sysUser.getDept().getDeptName());
                sendMsgArgsDTO.setVars(vars);
                log.info("发送人员电话号码={},所属大队={},参数内容={}", sysUser.getPhonenumber(),sysUser.getDept().getDeptName(),vars.toString());
                SendMsg.toSend(helper.enableSendMsg(),sendMsgArgsDTO);
            }
        }

        //个人测试
//        SendMsgArgsDTO sendMsgArgsDTO = new SendMsgArgsDTO();
//        sendMsgArgsDTO.setTellPhone("13668**3332");
//        sendMsgArgsDTO.setProject(SendMsgArgsDTO.PROJECT_SGTX);
//        JSONObject vars = new JSONObject();
//        vars.put("num",1);
//        vars.put("deptName","一大队");
//        sendMsgArgsDTO.setVars(vars);
//        SendMsg.toSend(sendMsgArgsDTO);

        log.info("发送短信完成");
    }
}
